package com.gba.mark.group.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gba.mark.group.entity.Group;
import com.gba.mark.group.mapper.GroupMapper;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.user.auth.utils.AuthUtils;
import com.gba.mark.user.entity.permission.UserRole;
import com.gba.mark.user.entity.user.User;
import com.gba.mark.user.mapper.permission.UserRoleMapper;
import com.gba.mark.user.service.permission.IUserRoleService;
import com.gba.mark.user.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService {

    @Autowired
    private IGroupService groupService;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private IUserRoleService userRoleService;

    @Resource
    private IUserService userService;

    @Override
    public Integer getGroupIdByAuthAndName(Authentication authentication, String groupName) {

        String username = AuthUtils.getUsername(authentication);
        User user = userService.getOne(new QueryWrapper<User>().eq("username", username));

        int adminId = user.getId();
        Group group = groupService.getOne(new QueryWrapper<Group>().eq("name",groupName).eq("admin_id",adminId));

        return group.getId();
    }

    @Override
    public boolean saveGroup(String language, String name, Integer size, Integer adminId) {
        Group group = new Group();
        // 判断组名是否重复
        if (this.baseMapper.selectOne(new QueryWrapper<Group>()
                .eq("admin_id", adminId)
                .eq("name", group.getName())) != null) {
            return false;
        }
        // 存入数据：小组名称、人数、语种、管理员
        group.setName(name);
        group.setSize(size);
        group.setLanguage(language);
        group.setAdmin_id(adminId);
        groupService.save(group);
        int groupId = group.getId();

//        int groupId =  this.baseMapper.insert(group);

        // 将管理员输入到组成员里面
        UserRole userRole = new UserRole();
        userRole.setGroupId(groupId);
        userRole.setUserId(adminId);
        userRole.setRoleId(1);
        userRoleService.save(userRole);
        return true;
    }

    @Override
    public boolean removeGroup(String groupName, int adminId) {

        return groupService.remove(new QueryWrapper<Group>()
                .eq("admin_id", adminId)
                .eq("name", groupName));

    }



    //查找组信息
    @Override
    public List<Group> searchGroup(int adminId) {
        return this.baseMapper.selectList(new QueryWrapper<Group>().eq("admin_id", adminId));
    }

    @Override
    public List<Group> listGroup(String username) {
        int userId = userService.getOne(new QueryWrapper<User>().eq("username", username)).getId();
        List<UserRole> groupList = userRoleMapper.selectList(
                new QueryWrapper<UserRole>().select("group_id").eq("user_id", userId)
        );
        if (groupList.size() == 0) {
            return new ArrayList<>();
        }
        List<Integer> groupIdList = new ArrayList<>();
        groupList.forEach(g -> groupIdList.add(g.getGroupId()));
        return this.baseMapper.selectList(new QueryWrapper<Group>().in("id", groupIdList));
    }

}